# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ConverterTextEdit(QtGui.QPlainTextEdit):
	def __init__(self):
		if sys.version_info < (3,):
			super(ConverterTextEdit, self).__init__()
		else:
			super().__init__()

	#def mouseDoubleClickEvent(self, mouseEvent):
	#    self.emit(SIGNAL("mouseDoubleClicked"), mouseEvent)

	def mousePressEvent(self, mouseEvent):
		if mouseEvent.button() == Qt.MidButton:
			self.emit(SIGNAL("mousePressed"), mouseEvent)
		else:
			QtGui.QPlainTextEdit.mousePressEvent(self, mouseEvent)

	def insertFromMimeData (self, mimeData):
		#super(ConverterTextEdit, self).insertFromMimeData(QMimeData)
		QtGui.QPlainTextEdit.insertFromMimeData(self, mimeData)
		self.emit(SIGNAL("pasted"))

class ConverterLineEdit(QtGui.QLineEdit):
	def __init__(self):
		if sys.version_info < (3,):
			super(ConverterLineEdit, self).__init__()
		else:
			super().__init__()

	def keyPressEvent (self, keyEvent):
		if keyEvent.key() == Qt.Key_Return or keyEvent.key() == Qt.Key_Enter:
			self.emit(SIGNAL("returned"), keyEvent)
		else:
			QtGui.QLineEdit.keyPressEvent(self, keyEvent)

class Window(QtGui.QWidget):
	def __init__(self):
		super(Window, self).__init__()

		self.dst2src = [0]
		self.src2dst = [0]

		self.setupGui()
		self.connect(self.buttonConvert, SIGNAL("clicked()"),self.doConvertButton)
		self.connect(self.lineEditInput, SIGNAL("returned"),self.doConvertButton)
		self.connect(self.textEditSrc, SIGNAL("mousePressed"),self.srcMap)
		self.connect(self.textEditDst, SIGNAL("mousePressed"),self.dstMap)
		self.connect(self.textEditSrc, SIGNAL("pasted"),self.doConvertButton)

		self.setWindowTitle("Log converter")

#   	 QCoreApplication.instance().setStyle(QtGui.QMotifStyle())

	def srcMap(self, mouseEvent):
		targetIndex = self.src2dst[self.textEditSrc.textCursor().blockNumber()]
		cursor = self.textEditDst.textCursor()
		distance = targetIndex - cursor.blockNumber()
		if distance > 0:
			cursor.movePosition(QtGui.QTextCursor.NextBlock, QtGui.QTextCursor.MoveAnchor, distance)
		else:
			cursor.movePosition(QtGui.QTextCursor.PreviousBlock, QtGui.QTextCursor.MoveAnchor, -distance)
			self.textEditDst.setTextCursor(cursor)
		self.textEditDst.verticalScrollBar().setSliderPosition(targetIndex)
		self.tab_widget.setCurrentIndex(1)
		self.textEditDst.setFocus(Qt.ActiveWindowFocusReason)
		self.textEditDst.centerCursor()

	def dstMap(self, mouseEvent):
		targetIndex = self.dst2src[self.textEditDst.textCursor().blockNumber()]
		cursor = self.textEditSrc.textCursor()
		distance = targetIndex - cursor.blockNumber()
		if distance > 0:
			cursor.movePosition(QtGui.QTextCursor.NextBlock, QtGui.QTextCursor.MoveAnchor, distance)
		else:
			cursor.movePosition(QtGui.QTextCursor.PreviousBlock, QtGui.QTextCursor.MoveAnchor, -distance)
		self.textEditSrc.setTextCursor(cursor)
		self.tab_widget.setCurrentIndex(0)
		self.textEditSrc.setFocus(Qt.ActiveWindowFocusReason)
		self.textEditSrc.centerCursor()

	def testCondition(self, line):
		if eval(str(self.lineEditInput.text()), {"_line": line}, {}):
			return line
		else:
			return ""

	def doConvertButton(self):
		self.textEditDst.clear()
		del self.src2dst[:]
		del self.dst2src[:]
		if sys.version_info < (3,):
			utf8Text = unicode(self.textEditSrc.toPlainText().toUtf8(), "utf-8")
		else:
			utf8Text = self.textEditSrc.toPlainText()
		for line in utf8Text.splitlines():
			if self.testCondition(line):
				self.textEditDst.appendPlainText(line)
				self.dst2src.append(len(self.src2dst))
				self.src2dst.append(len(self.dst2src) - 1)
			else:
				self.src2dst.append(len(self.dst2src))
		if len(self.dst2src) == 0:
			self.dst2src.append(0)
		self.tab_widget.setCurrentIndex(1)

	def setupGui(self):
		#self.textEditSrc = QtGui.QTextEdit()
		self.textEditSrc = ConverterTextEdit()
		self.textEditDst = ConverterTextEdit()
		self.lineEditInput = ConverterLineEdit()
		self.label1 = QtGui.QLabel("Filter condtion (startswith, endswith, count)")
		self.label2 = QtGui.QLabel("Original text")

		mainLayout = QtGui.QVBoxLayout()

		self.tab_widget = QtGui.QTabWidget()
		tab1 = QtGui.QWidget()
		tab2 = QtGui.QWidget()
		
		p1_vertical = QtGui.QVBoxLayout(tab1)
		p2_vertical = QtGui.QVBoxLayout(tab2)
		
		self.tab_widget.addTab(tab1, "Original")
		self.tab_widget.addTab(tab2, "Filtered")

		mainLayout.addWidget(self.tab_widget)

		self.lineEditInput.setText("_line.lower().count(\"jeseo\".lower())")

		self.buttonConvert = QtGui.QPushButton("Convert")
		p1_vertical.addWidget(self.buttonConvert)
		p1_vertical.addWidget(self.label1)
		p1_vertical.addWidget(self.lineEditInput)
		p1_vertical.addWidget(self.label2)
		p1_vertical.addWidget(self.textEditSrc)
		p2_vertical.addWidget(self.textEditDst)

		self.resize(700,650)
		self.setLayout(mainLayout)

if __name__ == '__main__':

	import sys

	app = QtGui.QApplication(sys.argv)
	window = Window()
	window.show()
	sys.exit(app.exec_())
